#include "usermanager.h"


UserManager::UserManager(QObject *parent)
    : QObject{parent}
{

}

QList<DictData>* UserManager::getDictData(QNetworkReply *reply)
{

    QByteArray bytes = reply->readAll();
    QJsonObject obj = QJsonDocument::fromJson(bytes).object();
    if(obj.value("code").toInt() != 200){
        QMessageBox::warning(0,"提示",obj.value("msg").toString());
        return nullptr;
    }
    QList<DictData> *dictDataList = new QList<DictData>;
    QJsonArray jsonDictDataArr = obj.value("data").toArray();
    for(int i = 0;i< jsonDictDataArr.size();i++){
        QJsonObject jsonDictData = jsonDictDataArr[i].toObject();
        DictData dictData;
        dictData.createBy = jsonDictData.value("createBy").toString();
        dictData.createTime = jsonDictData.value("createTime").toString();
        dictData.updateBy = jsonDictData.value("updateBy").toString();
        dictData.updateTime = jsonDictData.value("updateTime").toString();
        dictData.remark = jsonDictData.value("remark").toString();
        dictData.status = jsonDictData.value("status").toString();
        dictData.cssClass = jsonDictData.value("cssClass").toString();
        dictData.Ddefault = jsonDictData.value("Ddefault").toString();
        dictData.dictCode = jsonDictData.value("dictCode").toInt();
        dictData.dictLabel = jsonDictData.value("dictLabel").toString();
        dictData.dictSort = jsonDictData.value("dictSort").toInt();
        dictData.dictType = jsonDictData.value("dictType").toString();
        dictData.dictValue = jsonDictData.value("dictValue").toString();
        dictData.isDefault = jsonDictData.value("isDefault").toString();
        dictData.listClass = jsonDictData.value("listClass").toString();
        dictDataList->append(dictData);
    }
    return dictDataList;
}

QList<TreeSelect> *UserManager::getTreeSelect()
{
    QNetworkReply *reply = userManagerApi.deptTree();
    QByteArray bytes = reply->readAll();
    QJsonObject obj = QJsonDocument::fromJson(bytes).object();
    if(obj.value("code").toInt() != 200){
        QMessageBox::warning(0,"提示",obj.value("msg").toString());
        return nullptr;
    }
    QList<TreeSelect> *treeList = getTree(obj.value("data").toArray());
    return treeList;
}

QList<User> *UserManager::getList(QJsonObject obj)
{
    QList<User> *list = new QList<User>;
    QJsonArray userArr = obj.value("rows").toArray();
    for(int i = 0;i< userArr.size();i++){
        QJsonObject userObj = userArr.at(i).toObject();
        User *user = utils.getUserByJsonObj(userObj);
        list->append(*user);
    }
    return list;
}

QString UserManager::getInitPassword()
{
    QNetworkReply *reply = userManagerApi.initPassword();
    QByteArray bytes = reply->readAll();
    QJsonObject obj = QJsonDocument::fromJson(bytes).object();
    if(obj.value("code").toInt() != 200){
        QMessageBox::warning(0,"提示",obj.value("msg").toString());
        return nullptr;
    }
    return obj.value("msg").toString();
}

QList<TreeSelect> *UserManager::getTree(QJsonArray arr){
    QList<TreeSelect> *list = new QList<TreeSelect>;
    for(int i = 0;i< arr.size();i++){
        TreeSelect tree;
        tree.id = arr.at(i).toObject().value("id").toInt();
        tree.label = arr.at(i).toObject().value("label").toString();
        QJsonArray childrenJson = arr.at(i).toObject().value("children").toArray();
        QList<TreeSelect> *children = new QList<TreeSelect>;
        for(int i = 0;i< childrenJson.size();i++){
            children = getTree(childrenJson);
        }
        tree.children = *children;
        list->append(tree);
    }
    return list;
}


